<?php

/**
 * PaymentInvoice form.
 *
 * @package    erp-crm
 * @subpackage form
 * @author     Milwell H. Sia
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class PaymentInvoiceForm extends BasePaymentInvoiceForm
{
  public function configure()
  {
    if($this->isNew()){
      $this->validatorSchema['payment_iden'] = new sfValidatorString(array('required' => true));
      $this->setDefault('amount',0); 
    }
    
    $this->widgetSchema->setLabels(
      array(
        'amount'=>'Amount',
      )
    );
    /*
    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(
        array(
          new sfValidatorCallback(
            array(
              'callback'=>array(
                $this,'validateInvoiceId',
              ),
            )
          ),
          new sfValidatorCallback(
            array(
              'callback'=>array(
                $this,'validateCashAmount',
              ),
            )
          ),
        )
      )
    );
     
    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
*/
  }
  
  public function validateCashAmount($validator, $values){
    $error = array();
    $invoice_total = PaymentPeer::getInvoiceTotal($values['payment_iden']);
    if($this->cash_amount < ($values['amount']+$invoice_total))
      $error['amount'] = new sfValidatorError($validator,'Cash amount should be more than equal total invoice amount');

    if(count($error)>0)
      throw new sfValidatorErrorSchema($validator,$error);
    return $values;
  }
  
  public function validateInvoiceId($validator, $values){
    if(($values['amount'] == 0) && ($this->cash_amount == 0))
      return $values;
    $error = array();
    $c = new Criteria();
    $c->add(InvoicePeer::INVOICE_TYPE,$values['invoice_type']);
    $c->add(InvoicePeer::INVOICE_ID,$values['invoice_id']);
    $c->add(InvoicePeer::CUSTOMER_IDEN,$this->customer_iden);
    $invoice = InvoicePeer::doSelectOne($c);
    
    if (! $invoice)
      $error['invoice_type'] = new sfValidatorError($validator,'Invoice not found');
    else{
      $invoice_total_due = LogTransactionPeer::getInvoiceBalance($this->customer_iden,$invoice->getInvoiceIden());
      if( $invoice_total_due <= 0)
        $error['invoice_type'] = new sfValidatorError($validator,'Invoice has no balance');
      elseif( $invoice_total_due < $values['amount'])
        $error['amount'] = new sfValidatorError($validator,'Payment '.number_format($values['amount'],2).' is more than balance '.number_format($invoice_total_due,2));
    }
      
    if(count($error)>0)
      throw new sfValidatorErrorSchema($validator,$error);
    else
      $values['invoice_iden'] = $invoice->getInvoiceIden();
    return $values;
  }
  
  public function setCashAmount($amount){
    $this->cash_amount = $amount;
  }
  
  public function setCustomerIden($customer_iden){
    $this->customer_iden = $customer_iden;
  }
  
  public function setPaymentIdenDefault($payment_iden){
    $this->setDefault('payment_iden',$payment_iden);
  }
  
}
